package sisi.invoices;

import java.util.List;

import javax.persistence.EntityManager;

public class CustinvoicedetailController{

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Custinvoicedetail[] get(Integer orderNum, Integer idCustomer, Integer idSeq) {
		List<Custinvoicedetail> lista = getList(orderNum, idCustomer, idSeq);
			return lista.toArray(new Custinvoicedetail[0]);
		}

	@SuppressWarnings("unchecked")
	public List<Custinvoicedetail> getList(Integer orderNum, Integer idCustomer, Integer idSeq) {

		String cWhere = " WHERE c.order="+orderNum+" AND c.custid ="+idCustomer+" AND c.seq="+idSeq;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Custinvoicedetail as c " + cWhere 
					+ " order by c.recnum ");

			List<Custinvoicedetail> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	
	public Custinvoicedetail getCustinvoicedetail(String productname, Integer orderNum) {

		String cWhere = " WHERE c.order="+orderNum+" AND c.productname ='"+productname+"' ";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Custinvoicedetail as c " + cWhere );

			Custinvoicedetail custinvoicedetail = new Custinvoicedetail();
			if (q.getResultList().size()==1){			
				custinvoicedetail = (Custinvoicedetail)q.getSingleResult();
			} else if (q.getResultList().size()>1 ) {
				custinvoicedetail = (Custinvoicedetail)q.getResultList().get(0);
			}
			return custinvoicedetail;
		} finally {
			em.close();
		}
	}

	
	@SuppressWarnings("unchecked")
	public List<Custinvoicedetail> getList(Integer orderNum, Integer recNum) {

		String cWhere = " WHERE c.order="+orderNum+" AND c.recnum="+recNum;

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Custinvoicedetail as c " + cWhere 
					+ " order by c.recnum, c.seq ");

			List<Custinvoicedetail> lista = q.getResultList();
			return lista;
		} finally {
			em.close();
		}
	}

	
	public Custinvoicedetail getCustinvoicedetail(Integer productid, Integer orderNum) {

		String cWhere = " WHERE c.order="+orderNum+" AND c.productid = "+productid+" ";

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
				.createQuery("select c from Custinvoicedetail as c " + cWhere ); 

			
			Custinvoicedetail custinvoicedetail = new Custinvoicedetail();
			if (q.getResultList().size()==1){			
				custinvoicedetail = (Custinvoicedetail)q.getSingleResult();
			} else if (q.getResultList().size()>1 ) {
				custinvoicedetail = (Custinvoicedetail)q.getResultList().get(0);
			}
			return custinvoicedetail;

		} catch (Exception e) {
			return null;
			
		} finally {
			em.close();
		}
	}
	
	
	public Custinvoicedetail getxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Custinvoicedetail custinvoicedetailx = em.find(Custinvoicedetail.class, id);
			return custinvoicedetailx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Custinvoicedetail add(Custinvoicedetail custinvoicedetail) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(custinvoicedetail);
			em.getTransaction().commit();
			custinvoicedetail = refresh(custinvoicedetail);
		} finally {
			em.close();
			return custinvoicedetail;
		}
	}

	@SuppressWarnings("finally")
	public boolean update(Custinvoicedetail custinvoicedetail) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Custinvoicedetail custinvoicedetailx = em.find(Custinvoicedetail.class, custinvoicedetail.getId());
			custinvoicedetailx.setId(custinvoicedetail.getId());
			custinvoicedetailx.setType(custinvoicedetail.getType());
			custinvoicedetailx.setSeq(custinvoicedetail.getSeq());
			custinvoicedetailx.setNote(custinvoicedetail.getNote());
			custinvoicedetailx.setOrder(custinvoicedetail.getOrder());
			custinvoicedetailx.setRecnum(custinvoicedetail.getRecnum());
			custinvoicedetailx.setCurr(custinvoicedetail.getCurr());
			custinvoicedetailx.setCustid(custinvoicedetail.getCustid());
			custinvoicedetailx.setCustname(custinvoicedetail.getCustname());
			custinvoicedetailx.setCustnuminvoice(custinvoicedetail.getCustnuminvoice());
			custinvoicedetailx.setDtUp(custinvoicedetail.getDtUp());
			custinvoicedetailx.setExchangerate(custinvoicedetail.getExchangerate());
			custinvoicedetailx.setNfysNote(custinvoicedetail.getNfysNote());
			custinvoicedetailx.setPaidamount(custinvoicedetail.getPaidamount());
			custinvoicedetailx.setPaydt(custinvoicedetail.getPaydt());
			custinvoicedetailx.setStat(custinvoicedetail.getStat());
			custinvoicedetailx.setVatarticle(custinvoicedetail.getVatarticle());
			custinvoicedetailx.setBillitem(custinvoicedetail.getBillitem());
			custinvoicedetailx.setCusttaxed(custinvoicedetail.getCusttaxed());
			custinvoicedetailx.setCusttotsalevat(custinvoicedetail.getCusttotsalevat());
			custinvoicedetailx.setCustvat(custinvoicedetail.getCustvat());
			custinvoicedetailx.setCustvatperc(custinvoicedetail.getCustvatperc());
			custinvoicedetailx.setPaid(custinvoicedetail.getPaid());
			custinvoicedetailx.setPartno(custinvoicedetail.getPartno());
			custinvoicedetailx.setProductid(custinvoicedetail.getProductid());
			custinvoicedetailx.setProductname(custinvoicedetail.getProductname());
			custinvoicedetailx.setQuantity(custinvoicedetail.getQuantity());
			custinvoicedetailx.setTotalamount(custinvoicedetail.getTotalamount());
			custinvoicedetailx.setUm(custinvoicedetail.getUm());
			custinvoicedetailx.setUnitprice(custinvoicedetail.getUnitprice());
			custinvoicedetailx.setVatcode(custinvoicedetail.getVatcode());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
		return false;
		}
	}

@SuppressWarnings("finally")
public Custinvoicedetail refresh(Custinvoicedetail custinvoicedetail) {

		Custinvoicedetail custinvoicedetailx = custinvoicedetail;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			custinvoicedetailx = em.find(Custinvoicedetail.class, custinvoicedetail.getId());
			em.flush();
			em.refresh(custinvoicedetailx);
		} finally {
			em.close();
			return custinvoicedetailx;
		}
}

@SuppressWarnings("finally")
public boolean remove(Custinvoicedetail custinvoicedetail) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Custinvoicedetail custinvoicedetailx = em.find(Custinvoicedetail.class, custinvoicedetail.getId());
			em.remove(custinvoicedetailx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
}

}
//

